package com.example.icanlive;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.icanlive.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    // Used to load the 'icanlive' library on application startup.
    static {
        System.loadLibrary("icanlive");
    }

    //    private ActivityMainBinding binding;
    private SurfaceView surfaceView;
    private ICanPlayer iCanPlayer = new ICanPlayer();
    private Handler handler = new Handler();
    private int width = 0;
    private int height = 0;
    private int deviceW;
    private int deviceH;
    private Runnable updateWH = new Runnable() {
        @Override
        public void run() {
            if (width != 0 && height != 0) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(deviceW, deviceW * height / width);
                surfaceView.setLayoutParams(params);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        binding = ActivityMainBinding.inflate(getLayoutInflater());
//        setContentView(binding.getRoot());
        setContentView(R.layout.activity_main);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "申请权限", Toast.LENGTH_SHORT).show();
            // 申请 相机 麦克风权限
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.INTERNET,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        }

        this.onRestart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        // Example of a call to a native method
//        surfaceView = binding.surfaceView;
        surfaceView = findViewById(R.id.surfaceView);
        surfaceView.getHolder().addCallback(this);
        //hong dou
//        iCanPlayer.setDataSource("/sdcard/test1.mp4");
        //sky diary
//        iCanPlayer.setDataSource("/sdcard/test2.mp4");
        // douyin
//        iCanPlayer.setDataSource("/sdcard/test3.mp4");
        //CCTV1
//        iCanPlayer.setDataSource("http://39.134.115.163:8080/PLTV/88888910/224/3221225618/index.m3u8");
        //My Live
        iCanPlayer.setDataSource("http://139.159.226.207:1900/hls/test.m3u8");
//        iCanPlayer.setDataSource("rtmp://139.159.226.207:1935/hls/test");
        //sport
//        iCanPlayer.setDataSource("http://39.134.65.162/PLTV/88888888/224/3221225611/index.m3u8");

        iCanPlayer.setOnPrepareListener(new ICanPlayer.OnPrepareListener() {
            @Override
            public void onPrepare(int width, int height) {
//                Log.e("MainActivity", "prepare 2");
                Log.e("MainActivity", "width: " + width + ",height: " + height);
                MainActivity.this.width = width;
                MainActivity.this.height = height;
                Display display = getWindowManager().getDefaultDisplay();
                Point point = new Point();
                display.getSize(point);
                deviceW = point.x;
                deviceH = point.y;
                handler.post(updateWH);
                iCanPlayer.start();
            }
        });
        iCanPlayer.setOnProgressListener(new ICanPlayer.OnProgressListener() {
            @Override
            public void onProgress(int progress) {

            }
        });
        iCanPlayer.setOnProgressListener(new ICanPlayer.OnProgressListener() {
            @Override
            public void onProgress(int progress) {

            }
        });
        iCanPlayer.prepare();
    }

    @Override
    protected void onStop() {
        super.onStop();
        iCanPlayer.stop();
//        Log.e("MainActivity", "stop");
    }

    /**
     * A native method that is implemented by the 'icanlive' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {

    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        Surface surface = surfaceHolder.getSurface();
        iCanPlayer.setSurface(surface);
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

    }
}